<?php

namespace app\beautiful\controller\admin\member;

use app\beautiful\model\member\MemberCouponModel;

use app\BaseController;

use app\common\logic\DataLogic;
use think\facade\Db;


class Coupon extends BaseController{

    public function index()

    {

        $member_id =$this->request->param('member_id/d',0);

        $where=[

            ['mc.shop_id','=',$this->request->shopId]

        ];

        if(!empty($member_id)) $where[]=['mc.member_id','=',$member_id];


        $field="mc.member_coupon_id,mc.coupon_id,mc.name,mc.valid_bg_time,mc.valid_end_time,mc.coupon_money,
        mc.money_limit,mc.status,FROM_UNIXTIME(mc.add_time) as add_time_format ";

        $order=['mc.add_time' => 'desc'];

        $return = [];

        $return['total'] = Db::name('beautiful_member_coupon')->alias('mc')
            ->join('wyu_beautiful_coupon c','mc.coupon_id=c.coupon_id')
            ->field("c.type_id as coupon_type_id,$field")
            ->where($where)->count();

        $return['list'] = Db::name('beautiful_member_coupon')->alias('mc')
            ->join('wyu_beautiful_coupon c','mc.coupon_id=c.coupon_id ')
            ->field("c.type_id as coupon_type_id,$field")
            ->where($where)
            ->order($order)
            ->limit($this->request->limit)->page($this->request->page)
            ->select()
            ->toArray();

        /*$member_coupon=new MemberCouponModel();

        $member_coupon = $member_coupon->with(['coupon'=> function($query){
            $query->withField([
                'type_id as coupon_type_id',
            ]);
        }]);


        $order=['add_time'=>'desc'];

        $field="member_coupon_id,coupon_type_id,name,valid_bg_time,valid_end_time,coupon_money,money_limit,add_time,status";

        $append=['add_time_format'];

        $return = DataLogic::getDataList($member_coupon,$where,$order,$field,$this->request->limit,$this->request->page,$append);*/

        return $this->success($return);

    }

}

